Welcome to “Error Handling.” After watching this video, you will be able to: describe different HTTP status
that API services can return, explain how error handling works in Flask, and explain how to return
errors from your API endpoints. Every HTTP response contains a three-digit code
indicating different error and success status. The client is responsible for
consuming this error code. Valid response code ranges from 100 to 599. The error codes are organized
in categories of hundred each. Error codes from 100 to 199 indicate the
request has been received and are informational. Error codes from 200 to 299 indicate the request has been received and the
requested operation is successful. Error codes in the 300s indicate
there is a redirection on the server. Error codes in the range from 400
to 499 show an error in the request. Finally, error codes from 500 to 599
indicate an error on the server side. The APIs you will write in this course
will follow this nomenclature. For example, if the client sends a GET request for a
resource unavailable, you can send a 404 response back. You can respond with
status 400 for incorrect requests. Flask server automatically returns a 200 OK
status when you return from the @app.route method. 200 is also returned by default when you use
the jsonify() method to respond to a request. A successful response with a status code of 200
will be sent back when the given code executes. Your code can return a different
code than the default one. Flask allows you to send the status code
in a tuple along with the response. In the code, you send back an HTML response of "my first application in action!"
with a status code of 200. You can also use the make_response()
method to explicitly set the status code. This code returns the same HTML message and
HTTP status of 200 as the previous code, but here you use the make_response() method. Let's look at a few more examples of
codes you might use in this course. The status of 200 is returned by default.
It indicates that the request is successful. 201 tells the client that the server
has successfully created the resource. 202 indicates that the request is
accepted and is in the process, and is common for batch processing operations. The server returns 204 after completing a
request successfully; no content is returned. This status is useful in cases where you
don't want the browser to take any action. For example, the user stays on their current page. 400 indicates an invalid request. This status could imply the parameters are missing or improper or the request
is invalid in another way. 401 indicates the credentials
are missing or invalid. 403 implies that the client credentials
are not sufficient to fulfill the request. If the server is unable to find the
resource, it returns a 404 status. 405 indicates that the requested
operation is not supported. 500 is used when there is an error on the server. Now that you know the different
HTTP codes, you, as a developer, need to return the right code from the service. Let's look at an example. This search_response method looks for
the database's query parameter q. The service calls the mock "fetch_from_database"
method after parsing your query The code returns the resource to the client
if the resource exists. The server returns a code of 200 implicitly and returns
404 if the resource is not found. Let's call this endpoint using the curl program. Call the route without a query parameter. The curl command returns a message of "input
parameter missing" with a return status of 422. Next, call the route with the correct resource ID. The curl command returns the resource
as the body and a status of 200. Finally, call the route with a
resource that does not exist. The curl command returns a message of
"resource not found" with a status of 404. Flask provides a way to handle error
messages at the application level. Here, we see a method that processes the 404 error and returns a message "API not
found" with a status code of 404. Similarly, this snippet creates an error handler for 500 errors and returns a message of
"something went wrong on the server." In this video, you learned that: HTTP response requires a status code to indicate
what happens when the request processes. There are multiple classes of HTTP status codes
showing success, user error, or server error. Flask implicitly returns a success
code of 200 with the response. You can provide status codes explicitly. Flask provides application-level error handlers.